﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Cryptography.X509Certificates;
using Aga.Psad.Web.Helpers;
using Aga.Psad.Backend.Entities;
using Aga.Psad.Web.WDigDepFiel;
using System.Net;

namespace Aga.Psad.Web
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            using (var service = new WDigDepFiel.wDigDepFiel())
            {
                TicketAccesoHelper helper = new TicketAccesoHelper(this.Server);
            //    TicketAcceso ticket = helper.GetLoginTicket();

            //    Response.Write(
            //        String.Format(
            //            "Ticket de Acceso<br />ID: {0}<br />Token: {1}<br />Sign: {2}<br />Generado: {3}<br />Expira: {4}<br />",
            //            ticket.Id,
            //            ticket.Token,
            //            ticket.Sign,
            //            ticket.GenerationTime.ToLocalTime(),
            //            ticket.ExpirationTime.ToLocalTime()));
                

                try
                {
                    Autenticacion autentica = helper.GetAutentication();

                    string nroLegajo = "009001IC0100003R";
                    string cuitDeclarante = "23295021489";
                    string cuitPsad = "30708670681";
                    string cuitIE = "23295021489";
                    string codigo = "000";
                    DateTime fechaHoraAcept = DateTime.Now.AddMonths(-1);
                    string ticket = string.Empty;

                    ServicePointManager.ServerCertificateValidationCallback += 
                        new System.Net.Security.RemoteCertificateValidationCallback(customXertificateValidation);

                    Recibo recibo = service.AvisoRecepAcept(
                        autentica,
                        nroLegajo,
                        cuitDeclarante,
                        cuitPsad,
                        cuitIE,
                        codigo,
                        fechaHoraAcept,
                        ticket);

                    WsDummyResponse dummyResponse = service.Dummy();
                }
                catch (Exception ex)
                {
                    
                    throw ex;
                }
            }
        }

        private static bool customXertificateValidation(object sender, X509Certificate cert, X509Chain chain, System.Net.Security.SslPolicyErrors error)
        {
            string subject = "C=AR, L=Ciudad Autonoma de Buenos Aires, O=Administracion Federal de Ingresos Publicos, OU=Subdireccion General de Sistemas y Telecomunicaciones, CN=testdia.afip.gov.ar";
            if (cert.Subject == subject)
            {
                return true;
            }

            return false;
        }
    }
}
